package com.jeesite.modules.purchaseinbound.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.purchaseinbound.entity.PurchaseInboundHeader;
import com.jeesite.modules.purchaseinbound.dao.PurchaseInboundHeaderDao;
import com.jeesite.modules.purchaseinbound.entity.PurchaseInboundLines;
import com.jeesite.modules.purchaseinbound.dao.PurchaseInboundLinesDao;

/**
 * 采购入库单Service
 * @author 码脑洞
 * @version 2025-04-13
 */
@Service
public class PurchaseInboundHeaderService extends CrudService<PurchaseInboundHeaderDao, PurchaseInboundHeader> {
	
	@Autowired
	private PurchaseInboundLinesDao purchaseInboundLinesDao;
	
	/**
	 * 获取单条数据
	 * @param purchaseInboundHeader
	 * @return
	 */
	@Override
	public PurchaseInboundHeader get(PurchaseInboundHeader purchaseInboundHeader) {
		PurchaseInboundHeader entity = super.get(purchaseInboundHeader);
		if (entity != null){
			PurchaseInboundLines purchaseInboundLines = new PurchaseInboundLines(entity);
			purchaseInboundLines.setStatus(PurchaseInboundLines.STATUS_NORMAL);
			entity.setLinesList(purchaseInboundLinesDao.findList(purchaseInboundLines));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param purchaseInboundHeader 查询条件
	 * @param purchaseInboundHeader page 分页对象
	 * @return
	 */
	@Override
	public Page<PurchaseInboundHeader> findPage(PurchaseInboundHeader purchaseInboundHeader) {
		return super.findPage(purchaseInboundHeader);
	}
	
	/**
	 * 查询列表数据
	 * @param purchaseInboundHeader
	 * @return
	 */
	@Override
	public List<PurchaseInboundHeader> findList(PurchaseInboundHeader purchaseInboundHeader) {
		return super.findList(purchaseInboundHeader);
	}
	
	/**
	 * 查询子表分页数据
	 * @param purchaseInboundLines
	 * @param purchaseInboundLines page 分页对象
	 * @return
	 */
	public Page<PurchaseInboundLines> findSubPage(PurchaseInboundLines purchaseInboundLines) {
		Page<PurchaseInboundLines> page = purchaseInboundLines.getPage();
		page.setList(purchaseInboundLinesDao.findList(purchaseInboundLines));
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param purchaseInboundHeader
	 */
	@Override
	@Transactional
	public void save(PurchaseInboundHeader purchaseInboundHeader) {
		super.save(purchaseInboundHeader);
		// 保存 PurchaseInboundHeader子表
		for (PurchaseInboundLines purchaseInboundLines : purchaseInboundHeader.getLinesList()){
			if (!PurchaseInboundLines.STATUS_DELETE.equals(purchaseInboundLines.getStatus())){
				purchaseInboundLines.setHeaderId(purchaseInboundHeader);
				if (purchaseInboundLines.getIsNewRecord()){
					purchaseInboundLinesDao.insert(purchaseInboundLines);
				}else{
					purchaseInboundLinesDao.update(purchaseInboundLines);
				}
			}else{
				purchaseInboundLinesDao.delete(purchaseInboundLines);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param purchaseInboundHeader
	 */
	@Override
	@Transactional
	public void updateStatus(PurchaseInboundHeader purchaseInboundHeader) {
		super.updateStatus(purchaseInboundHeader);
	}
	
	/**
	 * 删除数据
	 * @param purchaseInboundHeader
	 */
	@Override
	@Transactional
	public void delete(PurchaseInboundHeader purchaseInboundHeader) {
		super.delete(purchaseInboundHeader);
		PurchaseInboundLines purchaseInboundLines = new PurchaseInboundLines();
		purchaseInboundLines.setHeaderId(purchaseInboundHeader);
		purchaseInboundLinesDao.deleteByEntity(purchaseInboundLines);
	}
	
}